package zhoug.demo.zui.ui;

import android.content.Context;
import android.graphics.Rect;
import android.text.Editable;
import android.util.AttributeSet;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListAdapter;

import androidx.appcompat.widget.AppCompatAutoCompleteTextView;

/**
 * @Author: zhoug
 * @Date: 2023-11-01
 * @Description:
 */
public class MyAppCompatAutoCompleteTextView extends AppCompatAutoCompleteTextView {

    public MyAppCompatAutoCompleteTextView(Context context) {
        super(context);
    }

    public MyAppCompatAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyAppCompatAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean enoughToFilter() {
        super.enoughToFilter();
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        Editable text = getText();
        if(focused && text!=null && text.toString().length()==0){
            ListAdapter adapter = getAdapter();
            if(adapter instanceof Filterable){
                Filter filter = ((Filterable) adapter).getFilter();
                filter.filter("");
            }
        }
    }
}
